/* library link for different output format.
 *
 * Copyright (c) 2012 Yongbo Tang
 *
 * This file is part of pylibMask.
 *
 * libMask is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * libMask is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with libMask.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "mask.h"
#include "gds.h"
#include "cif.h"
#include "egs.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

/* Set the current layer */
void layer(int n)
{  
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_layer(n);break;
    case FORMAT_EGS:
      egs_layer(n);break;
    case FORMAT_GDS:
      ;//  default      
    default:
      gds_layer(n);
  }
}

void mskpolygon3(int l, double x0, double y0, double x1, double y1,
    double x2, double y2)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskpolygon3(l, x0, y0, x1, y1, x2, y2);break;
    case FORMAT_EGS:
      egs_mskpolygon3(l, x0, y0, x1, y1, x2, y2);break;
    case FORMAT_GDS:
      ;//  default      
    default:
      gds_mskpolygon3(l, x0, y0, x1, y1, x2, y2);
  }
}

void mskpolygon4(int l, double x0, double y0, double x1, double y1,
    double x2, double y2, double x3, double y3)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskpolygon4(l, x0, y0, x1, y1, x2, y2, x3, y3);break;
    case FORMAT_EGS:
      egs_mskpolygon4(l, x0, y0, x1, y1, x2, y2, x3, y3);break;
    case FORMAT_GDS:
      ;//  default      
    default:
      gds_mskpolygon4(l, x0, y0, x1, y1, x2, y2, x3, y3);
  }
}

void mskpolygonN(int l, double *x, double *y, int n)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskpolygonN(l, x, y, n);break;
    case FORMAT_EGS:
      egs_mskpolygonN(l, x, y, n);break;
    case FORMAT_GDS:
      ;//  default      
    default:
      gds_mskpolygonN(l, x, y, n);
  }
}

void mskarc(int l, double w, double x1, double y1, double x2,
    double y2, double x3, double y3)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskarc(l, w, x1, y1, x2, y2, x3, y3);break;
    case FORMAT_EGS:
      egs_mskarc(l, w, x1, y1, x2, y2, x3, y3);break;
    case FORMAT_GDS:
      ;//  default      
    default:
      gds_mskarc(l, w, x1, y1, x2, y2, x3, y3);
  }
}

void mskarc2 (int l, double w, double r, double a1, double a2, double x1,
    double y1)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskarc2(l, w, r, a1, a2, x1, y1);break;
    case FORMAT_EGS:
      egs_mskarc2(l, w, r, a1, a2, x1, y1);break;
    case FORMAT_GDS:
      ;//  default      
    default:
      gds_mskarc2(l, w, r, a1, a2, x1, y1);
  }
}

void mskcircle(int l, double w, double x1, double y1, double x2, double y2)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskcircle(l, w, x1, y1, x2, y2);break;
    case FORMAT_EGS:
      egs_mskcircle(l, w, x1, y1, x2, y2);break;
    case FORMAT_GDS:
      ;//  default      
    default:
      gds_mskcircle(l, w, x1, y1, x2, y2);
  }
}

/* polyline, wire */
void mskpolystart(int l, double w)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskpolystart(l, w);break;
    case FORMAT_EGS:
      egs_mskpolystart(l, w);break;
    case FORMAT_GDS:
      ;//  default      
    default:
      gds_mskpolystart(l, w);
  }
}

void mskpolypoint(int l, double x, double y)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskpolypoint(l, x, y);break;
    case FORMAT_EGS:
      egs_mskpolypoint(l, x, y);break;
    case FORMAT_GDS:
      ;//gds_mskpolypoint(l, x, y);break;      
    default:
      gds_mskpolypoint(l, x, y);
  }
}

void mskpolygonend(int l)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskpolygonend(l);break;
    case FORMAT_EGS:
      egs_mskpolygonend(l);break;
    case FORMAT_GDS:
      ;//gds_mskpolygonend(l);break;      
    default:
      gds_mskpolygonend(l);
  }
}

void mskpolyend(int l)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskpolyend(l);break;
    case FORMAT_EGS:
      egs_mskpolyend(l);break;
    case FORMAT_GDS:
      ;//gds_mskpolyend(l);break;      
    default:
      gds_mskpolyend(l);
  }
}

void mskpolygonstart(int l)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskpolygonstart(l);break;
    case FORMAT_EGS:
      egs_mskpolygonstart(l);break;
    case FORMAT_GDS:
      ;//gds_mskpolygonstart(l);break;      
    default:
      gds_mskpolygonstart(l);
  }
}

/*
bool CheckExtendName(const char *pszStr,const char *pszExd)
{//extract the extension and compare it with set name.  
 char *pt=(char*)pszStr;
 
 pt +=strlen(pszStr);
 while(pt!=(char*)pszStr && *pt-- !='.');

 if(*(++pt) !='.')
  return false;
 pt++;

  //convert into lower case for comparison
 return (strcmp(strlwr(pt),strlwr((char *)pszExd))==0)
}*/

void msk_main_header(const char *filename)
{
  char *pt = (char *)filename;
  
  pt +=strlen(filename);//come to the end
  while(pt!=(char*)filename && *pt-- !='.');//search the '.' or come to the first character.  
  //if(*(++pt) !='.') output_format = FORMAT_GDS;//default
  pt++;//goto the '.'
  pt++;//skip the '.'
  
  if(strcmp(strlwr(pt),"cif")==0)
  {
    output_format = FORMAT_CIF; 
  }
  else if (strcmp(strlwr(pt),"egs")==0)
  {
    output_format = FORMAT_EGS; 
  }
  else//Default is GDS
  {
    output_format = FORMAT_GDS; 
  }
  
  //fprintf(stderr, "%s, %s, %d\n", filename, pt, output_format);
  
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_msk_main_header(filename);break;
    case FORMAT_EGS:
      egs_msk_main_header(filename);break;
    case FORMAT_GDS:
      ;//gds_msk_main_header(filename);break;
    default:
      gds_msk_main_header(filename);  
  }
}

void msk_sublayout_header(const char *layout)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_msk_sublayout_header(layout);break;
    case FORMAT_EGS:
      egs_msk_sublayout_header(layout);break;
    case FORMAT_GDS:
      ;//gds_msk_sublayout_header(layout);break;      
    default:
      gds_msk_sublayout_header(layout);  
  }
}

void msk_sublayout_trailer()
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_msk_sublayout_trailer();break;
    case FORMAT_EGS:
      egs_msk_sublayout_trailer();break;
    case FORMAT_GDS:
      ;//gds_msk_sublayout_trailer();break;      
    default:
      gds_msk_sublayout_trailer();  
  }
}

void msk_trailer()
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_msk_trailer();break;
    case FORMAT_EGS:
      egs_msk_trailer();break;
    case FORMAT_GDS:
      ;//gds_msk_trailer();break;      
    default:
      gds_msk_trailer();  
  }
}

void mskline(int l, double w, double xb, double yb, double xe, double ye)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_mskline(l, w, xb, yb, xe, ye);break;
    case FORMAT_EGS:
      egs_mskline(l, w, xb, yb, xe, ye);break;
    case FORMAT_GDS:
      ;//gds_mskline(l, w, xb, yb, xe, ye);break;      
    default:
      gds_mskline(l, w, xb, yb, xe, ye);
  }
}

void msklayout(const int layoutnr, const char *name, double a, double x,
    double y, int flip)
{
  switch(output_format)
  {
    case FORMAT_CIF:
      cif_msklayout(layoutnr, name, a, x, y, flip);break;
    case FORMAT_EGS:
      egs_msklayout(layoutnr, name, a, x, y, flip);break;
    case FORMAT_GDS:
      ;//gds_msklayout(layoutnr, name, a, x, y, flip);break;      
    default:
      gds_msklayout(layoutnr, name, a, x, y, flip);
  }
}
